// LuaMarionette.cpp : Defines the initialization routines for the DLL.
//

#include "stdafx.h"
#include "LuaAdapter.h"
#include "LuaMarionette.h"
#include "ConsoleDlg.h"
#include<comutil.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

#define TIMER_READCOMMAND 12345

//
//TODO: If this DLL is dynamically linked against the MFC DLLs,
//		any functions exported from this DLL which call into
//		MFC must have the AFX_MANAGE_STATE macro added at the
//		very beginning of the function.
//
//		For example:
//
//		extern "C" BOOL PASCAL EXPORT ExportedFunction()
//		{
//			AFX_MANAGE_STATE(AfxGetStaticModuleState());
//			// normal function body here
//		}
//
//		It is very important that this macro appear in each
//		function, prior to any calls into MFC.  This means that
//		it must appear as the first statement within the 
//		function, even before any object variable declarations
//		as their constructors may generate calls into the MFC
//		DLL.
//
//		Please see MFC Technical Notes 33 and 58 for additional
//		details.
//

// CLuaMarionetteApp

BEGIN_MESSAGE_MAP(CLuaMarionetteApp, CWinApp)
END_MESSAGE_MAP()

static int l_Output( lua_State* L )
{
	const char* theStr=luaL_checkstring(L,1);
	CString str(theStr);
	lua_pushstring(L,"Console");
	lua_gettable(L,LUA_REGISTRYINDEX);
	CConsoleDlg* console=(CConsoleDlg*)lua_touserdata(L,-1);
	console->Output(str);
	return 0;
}

// CLuaMarionetteApp construction

CLuaMarionetteApp::CLuaMarionetteApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
	m_script=NULL;
	m_Console=NULL;
}

// The one and only CLuaMarionetteApp object

CLuaMarionetteApp theApp;

static void TestPrintByLuaAsm(int x,int y)
{
	int c=x+y;
}

static void TestPrintString(const char* str)
{
	CString outer(str);
}

static int xx=5;
static int yy=6;

// CLuaMarionetteApp initialization
BOOL CLuaMarionetteApp::InitInstance()
{
	CWinApp::InitInstance();
	m_script=new CLua();
	m_script->Init();
	m_script->ExportFunction("output",l_Output);
	m_Console = new CConsoleDlg();
	m_script->SetOutputConsole(m_Console);
	m_Console->Create();
	m_Console->ShowWindow(SW_NORMAL);
	m_script->RunFile("abc.lua");
	m_script->RunScript("PrintHello()");
	::SetTimer(NULL,TIMER_READCOMMAND,10,CLuaMarionetteApp::OnTimer);
	DWORD testptr=(DWORD)TestPrintByLuaAsm;
	DWORD testprintptr=(DWORD)TestPrintString;
	DWORD xxptr=(DWORD)&xx;
	DWORD yyptr=(DWORD)&yy;
	return TRUE;
}

CLuaMarionetteApp::~CLuaMarionetteApp()
{
	m_script->UnInit();
	delete m_script;
}

void CALLBACK CLuaMarionetteApp::OnTimer( HWND, UINT, UINT_PTR, DWORD )
{
	CString pCommand=theApp.m_Console->ReadCommand();
	if(pCommand.IsEmpty())
		return;
	_bstr_t w2a(pCommand.GetBuffer());
	char* command = (char*)w2a;
	if(!theApp.m_script->RunScript(command))
	{
		CString errStr(theApp.m_script->GetLastError().c_str());
		theApp.m_Console->Output(errStr,RGB(0xff,0x10,0x10));
	}
}


